Lab 14

  1. Write title lines for the functions that are called by the following main program. Do not supply the blocks for the functions. Your title lines must allow for any indicated types of output.
  2. int main() { cout << firstDigit(65536) << endl; // prints: Six cout << undouble(11223344); // prints: 1234 cout << halfString("Freddy") << endl; // prints: Fre sort("Kelly", "Max", "Freddy", "Jack"); // prints: Freddy Jack Kelly Max string a = randomWord(); // assigns a random value return 0; }

    1. Title line for firstDigit
    2. Title line for undouble
    3. Title line for halfString
    4. Title line for sort
    5. Title line for randomWord
  3. Write title lines for the following functions. Do not supply the blocks for the functions.
    1. A function called sumDigits which returns the sum of the digits of an integer.
    2. A function called isSmall that returns an answer of true if a double precision parameter has a value between 0 and 0.001. (It returns false otherwise.)
    3. A function called randomLetter which generates and returns a random letter of the alphabet. (The output is to be a single character between 'A' and 'Z'.)
    4. A function called sort3 which is to change a collection of three input values so that they appear in increasing order.
  4. Write blocks of code to perform the functions used in the following main program. Your blocks must match the given title lines. Each block should be a short function of only a few lines.
  5. int main() { // (a) Print the number of odd arguments, here 1 cout << numberOdd(7,8) << endl; // (b) Print closest integer here 4 cout << closest(3.75) << endl; // (c) Print maximum value, here 4 cout << max(3, 1, 4, 1) << endl; // (d) Print the first digit, assume argument is positive. Here 1. cout << firstDigit(19683) << endl; return 0; }

    1. int numberOdd(int x, int y)
    2. int closest(double x)
    3. int max(int a, int b, int c, int d)
    4. int firstDigit(int x)
  6. Write a function called oddLessEven that returns the sum of the odd valued digits minus the sum of the even valued digits in a positive integer parameter. For example, a program that uses the function oddLessEven follows. (Submit a complete C++ program that contains the main function below and your oddLessEven function in a single file.)
  7. int main() { cout << oddLessEven(23) << endl; // prints 1 cout << oddLessEven(1234) << endl; // prints -2 cout << oddLessEven(777) << endl; // prints 21 return 0; }
  8. Write a function called sumRatios. The function has two integer parameters that are positive and have the same number of digits all of which are non-zero. It prints the sum of the ratios of corresponding digits. For instance sumRatios(132,568) calculates 1/5 + 3/6 + 2/8 and returns an answer of 0.95. (Submit a complete C++ program that contains the main function below and your sumRatios function in a single file.)
  9. int main() { cout << sumRatios(132, 568) << endl; // prints 0.95 return 0; }